import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.util.TreeSet;

/**
 * @author blank
 *
 */
/*
 * 项目的主程序
 * 
 */
public class mainApp {
	//控制考试时间的开关
	static boolean flag =true;
	static int score;
	static Scanner scanner = new Scanner(System.in);
	static TreeSet<Exercises> questions = new TreeSet<Exercises>();
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	// static ArrayList<Exercises> questions =new ArrayList<Exercises>();
//	static ArrayList<Exercises> questions2 =new ArrayList<Exercises>();

	public static void main(String[] args) throws SQLException {
		System.out.println("--------在线答题系统--------");
		int choose = -1;
		while (true) {
			System.out.println("--------首页--------");
			System.out.println("1 录入题目");
			System.out.println("2 开始考试");
			System.out.print("选择：");
			try {
				choose = Integer.parseInt(scanner.nextLine());
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				System.out.println("错误输入，请输入数字");
			}
			if (choose == 1) {
				addExercises();
			} else if (choose == 2) {
				startAnswer();
			} else {
				System.out.println("输入错误");
			}
		}
	}

	/**
	 * 开始考试
	 * @throws SQLException 
	 */
	private static void startAnswer() throws SQLException {
		// TODO Auto-generated method stub
		System.out.println("--------开始考试--------");
		// 计时线程
		Thread t = new myThread();
		t.start();

		// 从本地读取习题
		readQuestion();
		//出题
		if (questions.size()<10) {
			System.out.println("暂时还不可以考试");
		}else {
			score =0;
			questionLoad();
			System.out.println("考试结束，得分："+score+" 用时");
			System.out.println("请输入你的姓名，确认身份。");
			String name;
			name=scanner.nextLine();
			String usetime="2020-10-06 17:03:18";
			
			
			Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false","root","root");
			connection.setAutoCommit(false);
			String sql="insert into score (name,score,usetime) value('"+name+"','"+score+"','"+usetime+"')";
			
			Statement st=connection.createStatement();
			int i= st.executeUpdate(sql);
			
			if (i==1) {
				System.out.println("添加成功");
			}
			connection.commit();
		st.close();
		connection.close();
			
			
		}

	}

	/**
	 *  出题
	 */
	private static void questionLoad() {
		// TODO Auto-generated method stub
		int randomNum = (int) (Math.random() *9+1);
		for (Exercises exercises : questions) {
			if (exercises.getNumber()>=randomNum) {
				System.out.print("第" + exercises.getNumber() + "题：");
				System.out.println(exercises.getQuestion());
				answerQuestion();
			}
		}
		
	}

	/**
	 * 
	 */
	private static void answerQuestion() {
		// TODO Auto-generated method stub
		String answer;
		System.out.println("请输入你的答案：");
		answer=scanner.nextLine();
		score+=10;
	}

	/**
	 *  读取题目
	 */

	private static void readQuestion() {
		
		try {
			FileInputStream filein = new FileInputStream("Question.txt");
			ObjectInput in = new ObjectInputStream(filein);
			
			Object o = in.readObject();
			@SuppressWarnings("unchecked")
			TreeSet<Exercises> questionsTemp = (TreeSet<Exercises>) o;
			
			// 从文件反序化多个对象，然后转换成treeset
			

			for (Exercises exercisesTemp : questionsTemp) {
				questions.add(exercisesTemp);
			}
			
			/*
			 * 用于测试
			 * for (Exercises exe : questions2) { System.out.println(exe); }
			 */

			in.close();
			filein.close();
			System.out.println();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 添加题目
	 */
	private static void addExercises() {
		// TODO Auto-generated method stub
		// 新建的题目
		Exercises exercise = new Exercises();

		// 输入的序号
		int inNumber;
		// 输入的题目内容
		String inQuestion;
		System.out.println("--------录入题目--------");
		try {
			// 添加题目序号
			System.out.print("请输入题目序号：");
			inNumber = Integer.parseInt(scanner.nextLine());
			exercise.setNumber(inNumber);
			// 添加题目
			System.out.println("请输入题目内容：");
			inQuestion = scanner.nextLine();
			exercise.setQuestion(inQuestion);
			System.out.println("添加成功");
		} catch (NumberFormatException e1) {
			// TODO Auto-generated catch block
			System.out.println("错误输入，请输入数字");
		}

		// 把题目加入列表
		questions.add(exercise);

		// 把写习题写入文件
		writeQuerstion();

	}

	/**
	 * 写入文件
	 */
	private static void writeQuerstion() {
		try {
			FileOutputStream outfile = new FileOutputStream("Question.txt");
			ObjectOutputStream out = new ObjectOutputStream(outfile);
			out.writeObject(questions);
			out.close();
			outfile.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
